/*
 * @Author: intellectual-seeker 3336339701@qq.com
 * @Date: 2025-08-18 17:11:14
 * @LastEditors: intellectual-seeker 3336339701@qq.com
 * @LastEditTime: 2025-08-18 17:12:48
 * @FilePath: /code/lesson26/ShmServer.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "Comm.hpp"
#include "Fifo.hpp"

//Server端：创建删除共享内存、读数据
int main()
{
    //创建管道文件
    Fifo fifo(PATH);

    //创建key值
    key_t key = CreateShmKeyOrDie();
    cout << "Server key : " << ToHex(key) << endl;

    //根据key值创建共享内存
    int shmid = CreateShm(key, default_size);
    cout << "Server shmid : " << shmid << endl;

    //将共享内存进行挂载，获取共享内存的地址
    char* addr = (char*)ShmAttach(shmid);//获取地址时必须确定指针类型

    //打印一下共享内存的属性信息
    //ShmDebug(shmid);

    //以读方式打开管道文件
    Sync sync;
    sync.OpenReadOrDie();

    //读取操作，默认传递的数据是字符串
    while(1)
    {
        if(!sync.Wait())//等待被唤醒，写端关闭停止读取
            break;

        cout << "Content : " << addr << endl;
    }

    //解绑共享内存
    ShmDettach(addr);

    sleep(5);

    //删除共享内存
    DeleteShm(shmid);

    return 0;
}